<template>
  <div>
    <h5 class="text-muted">当前任务：签约登记</h5>
    <el-form @submit.native.prevent class="query-table-form" :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="企业名称查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          placeholder="身份证号码查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="经营者名称查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox
          v-model="params.has_ele_fee"
          border
          :true-label="1"
          :false-label="0"
          @change="getData"
        >
          电表
        </el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" class="table-sm" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>

      <template v-slot:townStatus="scope">
        <span class="badge" :class="`badge-${scope.row.townStatusStyle}`">
          {{ scope.row.townStatusLabel }}
        </span>
      </template>

      <template v-slot:bankStatus="scope">
        <span class="badge" :class="`badge-${scope.row.bankStatusStyle}`">
          {{ scope.row.bankStatusLabel }}
        </span>
      </template>

      <template v-slot:cityStatus="scope">
        <span class="badge" :class="`badge-${scope.row.cityStatusStyle}`">
          {{ scope.row.cityStatusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <register-file
      :related-data="editRow"
      :view-mode="editViewMode"
      :visible.sync="editVisible"
      @success="handleFileSuccess"
    ></register-file>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import RegisterFile from '@/credit/views/county/mission/RegisterFile'
import { enumOrgClass } from '@/credit/plugins/enum'
import {
  enumApprovalRegister,
  enumBankLenderStatus,
  enumReinsuranceStatus,
} from '@/gov/plugins/enum'

const enumStatusStyle = {
  0: 'info',
  1: 'success',
  2: 'danger',
}

export default {
  components: {
    RegisterFile,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        lender_idcard: '',
        customer_name: '',
        corp_name: '',
        'res_status.eq': 1,
        'submit_type.eq': 4,
        'service_id.eq': '',
      },
      loading: false,

      tableName: 'customer_related_register_view',
      tableData: [],
      totalCount: 0,
      tableColumns: [
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        {
          attrs: { label: '组织形式', prop: 'org_class', align: 'center' },
          process: v => enumOrgClass[v] || '-',
        },
        { attrs: { label: '经营者姓名', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        { attrs: { label: '放款金额', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '贷款年限', prop: 'load_years', align: 'center' } },
        {
          attrs: { label: '到期时间', prop: 'load_end_date', align: 'center' },
          process: v => this.$utils.dateFormat(v, 'yyyy-MM-dd') || '-',
        },
        {
          attrs: { label: '银行签约', prop: 'bank_lender_status', align: 'center' },
          slot: 'bankStatus',
        },
        {
          attrs: { label: '担保签约', prop: 'approval_register_status', align: 'center' },
          slot: 'townStatus',
        },
        {
          attrs: { label: '市农担联保', prop: 'reinsurance_status', align: 'center' },
          slot: 'cityStatus',
        },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            {
              content: '登记',
              icon: 'fa fa-edit',
              event: this.handleEdit,
              // ifRender: row => {
              //   return this.isUserTown && +row.approval_register_status === 0
              // }
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      // 登记
      editViewMode: false,
      editVisible: false,
      editRow: {},
    }
  },
  computed: {
    ...mapGetters(['user', 'userId', 'clientId', 'job']),
    isManager() {
      return ['org_credit_super', 'org_c_manage', 'org_r_emp', 'org_r_manage', 'org_corp'].includes(
        this.job
      )
    },
    // isUserTown() {
    //   return this.user.userRegionLever === 3
    // }
  },
  watch: {
    $route(route) {
      if (route.name === 'gxsRegister') {
        this.getData()
      }
    },
  },
  created() {
    this.params['recommend_service.eq'] = this.clientId
    // if (this.isUserTown) {
    //   this.params['service_id.eq'] = this.clientId
    // } else {
    //   this.params['recommend_service.eq'] = this.clientId
    // }
    this.getData()
  },
  mounted() {},
  methods: {
    async getData() {
      const params = { ...this.params }
      const status = this.$route.query.status
      if (status !== undefined) {
        params['approval_register_status.eq'] = status
      }
      if (!this.isManager) {
        params['orWhere.res_a.eq'] = this.userId
        params['orWhere.res_b.eq'] = this.userId
      }

      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.totalCount = data.totalCount
      this.tableData = (data.data || []).map(item => {
        item.townStatusLabel = enumApprovalRegister[item.approval_register_status] || '-'
        item.townStatusStyle = enumStatusStyle[item.approval_register_status] || ''

        item.cityStatusLabel = enumReinsuranceStatus[item.reinsurance_status] || '-'
        item.cityStatusStyle = enumStatusStyle[item.reinsurance_status] || ''

        item.bankStatusLabel = enumBankLenderStatus[item.bank_lender_status] || '-'
        item.bankStatusStyle = enumStatusStyle[item.bank_lender_status] || ''

        return item
      })
    },
    handleEdit(row) {
      this.editViewMode = false
      this.editRow = row
      this.editVisible = true
    },
    handleView(row) {
      this.editViewMode = true
      this.editRow = row
      this.editVisible = true
    },
    handleFileSuccess() {
      this.$store.dispatch('getTaskCountGxs')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped></style>
